package com.ruoyi.web.controller.vantController.orderController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderRetreat;
import com.ruoyi.system.domain.zhuangxiumain.vo.OrderVo;
import com.ruoyi.system.service.vantservice.customerervice.ApiLocalFabulousService;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderMasterService;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderRetreatService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by 惠普 on 2020/12/26 19:01
 */
@RestController
@RequestMapping("/api/order/retreat")
@CrossOrigin
public class ApiOrderRetreatController {
    @Resource
    private ApiOrderRetreatService apiOrderRetreatService;
    @Resource
    private ApiOrderMasterService apiOrderMasterService;
    @Resource
    private ApiLocalFabulousService apiLocalFabulousService;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;

    /**
     * 添加退货信息
     * @param orderRetreat
     * @return
     */
    @PostMapping("/orderRetreat")
    public AjaxResult addOrderRetreat(@RequestBody OrderRetreat orderRetreat){
        try {
            // 获取前台登录用户信息
            CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
            orderRetreat.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
            orderRetreat.setOrderRetreatApplicationTime(new Date());
            int row = apiOrderRetreatService.addOrderRetreat(orderRetreat);
            OrderVo orderVo = new OrderVo();
            if(row==1){
                orderVo.setOrderId(orderRetreat.getOrderId());
                orderVo.setOrderStatus(8);
            }
            return AjaxResult.success(apiOrderMasterService.updateOrderVo(orderVo));
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

    /**
     * 多图片上传
     * @return
     */
    @PostMapping("/upload")
    public Map<String, Object> up(@RequestParam("files") MultipartFile[] files){
        return apiLocalFabulousService.uploadImages(files);
    }


}
